package com.biqee.action.admin;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;

import com.biqee.action.base.BaseAction;
import com.biqee.service.crawler.BqIndexBuilder;
import com.biqee.util.StoreUtil;
import com.biqee.util.StringUtil;
import com.ibatis.common.logging.Log;
import com.ibatis.common.logging.LogFactory;

public class TaskAction extends BaseAction{
	Log log = LogFactory.getLog(TaskAction.class);

	private static final long serialVersionUID = -271061134288684156L;
	
	private static final String storeRoot = StoreUtil.STORE_ROOT+"/hishtml";
	
	BqIndexBuilder bqIndexBuilder ;
	
	private String destDir ;
	
	private List<String> subFolders;
	
	public String execute() {
		intiSubFolders();
		return "index";
	}
	
	
	private void intiSubFolders(){
		File file = new File(storeRoot);
		if(file.exists()){
			File[] files = file.listFiles((FileFilter) DirectoryFileFilter.INSTANCE);
			if(files!=null&&files.length>0){
				subFolders = new ArrayList<String>();
				for(File subFolder:files){
					subFolders.add(subFolder.getName());
				}
			}
			
		}
		
	}
	
	public String deleteDir(){
		if(StringUtil.isNotBlank(destDir)){
			File file = new File(storeRoot+"/"+destDir);
			if(file.exists()){
				try {
					FileUtils.forceDelete(file);
				} catch (IOException e) {
					log.warn(e.getMessage());
				}
			}
		}
		return INPUT ;
	}
	
	public String staticHtmTask(){
		if(StringUtil.isNotBlank(destDir)){
			try {
				bqIndexBuilder.backup(destDir);
			} catch (IOException e) {
				log.warn(e.getMessage());
			}
		}
		
		bqIndexBuilder.build("http://www.biqee.com/biqee/homepage!index.jspa");
		return SUCCESS;
	}


	public void setBqIndexBuilder(BqIndexBuilder bqIndexBuilder) {
		this.bqIndexBuilder = bqIndexBuilder;
	}


	public List<String> getSubFolders() {
		return subFolders;
	}


	public String getDestDir() {
		return destDir;
	}


	public void setDestDir(String destDir) {
		this.destDir = destDir;
	}
	
	

	

}
